﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using pepitoEngine;
namespace WindowsGame1
{
    class Scene3D : Scene
    {
        Camera mCurrentCamera;
        //-Lista de camaras
        List<Camera> Cameras = new List<Camera>();
        List<Camera> camerasToAdd = new List<Camera>();
        List<Camera> camerasToRemove = new List<Camera>();
        //-Lista de actores
        List<Actor3d> Actors = new List<Actor3d>();
        List<Actor3d> actorsToAdd = new List<Actor3d>();
        List<Actor3d> actorsToRemove = new List<Actor3d>();
        //------------------
        // Actor principal, lo tenemos fuera de la lista de actores
        Actor3d mMainActor;

        InputManager Input = new InputManager();

        ContentManager Content;
 //       public enum CameraMode
 //       {
 //           FirstPerson,
 //           ThirdPerson
 //       }

        public Scene3D(ContentManager content)
        {
            Content = content;
            Actor3d x = new Actor3d(Content, "Airplane/airplane 2");
            x.SetPosition(5, 0, 5);
            Actors.Add(x);
            Actor3d y = new Actor3d(Content, "ironman/Iron Man");
            y.SetPosition(10, -5, 20);
            Actors.Add(y);

            Camera c = new Camera();
            Cameras.Add(c);
            mCurrentCamera = c;

           // mMainActor = new Actor3d(Content, "elf/elf_archer_static");
            mMainActor = new Actor3d(Content, "Airplane/airplane 2");
            c.setActor(mMainActor);
        }

        public override void update(int dTime)
        {
              //updates!!!
            foreach (Camera c in Cameras)
            {
                c.update(dTime);
                if (c.readyToexit)
                {
                    camerasToRemove.Add(c);
                }
            }
            foreach (Actor3d a in Actors)
            {
                a.update(dTime);
                if (a.isIdle)
                {
                    actorsToRemove.Add(a);
                }
            }


            foreach (Actor3d a in actorsToAdd)
            {
                Actors.Add(a);
            }

            foreach (Actor3d a in actorsToRemove)
            {
                Actors.Remove(a);

            }
            actorsToAdd.Clear();
            actorsToRemove.Clear();
            
            base.update(dTime);
        }

        public override void draw()
        {
            foreach (Actor3d a in Actors) { 
                a.Draw(mCurrentCamera);
            }
            if (mCurrentCamera.mCameraMode==CameraMode.ThirdPerson)
                mMainActor.Draw(mCurrentCamera);

            base.draw();
        }

        public override void handleInput(InputManager Input)
        {
            if (Input.isNewKeyPress(Keys.Tab))
            {
                mCurrentCamera.SwitchMode();
            }
            if (Input.isKeyPressed(Keys.A))
            {
                mMainActor.Rotate(0,0.01f,0);
            }
            if (Input.isKeyPressed(Keys.D))
            {
                mMainActor.Rotate(0, -0.01f, 0);
            }
            if (Input.isKeyPressed(Keys.W))
            {
                mMainActor.Walk(0.1f);
            }
            if (Input.isKeyPressed(Keys.S))
            {
                mMainActor.Walk(-0.1f);
            }
            base.handleInput(Input);
        }
    }
}

